use std::ffi::c_void;

use windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow;

// pub fn get_main_window_handle() {
//     unsafe {
//         let mut info: GUITHREADINFO = std::mem::zeroed();
//         let id = std::thread::current().id().as_u64().get() as u32;
//         if GetGUIThreadInfo(id, &mut info).as_bool() {
//             dbg!(info);
//         } else {
//             println!("GetGUIThreadInfo");
//         }
//     }
// }

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32WindowHandle};

struct ForegroundWindow;
use futures::executor::block_on;

unsafe impl HasRawWindowHandle for ForegroundWindow {
    fn raw_window_handle(&self) -> RawWindowHandle {
        let hwnd = unsafe { GetForegroundWindow() };
        let mut handle = Win32WindowHandle::empty();
        handle.hwnd = hwnd.0 as *mut c_void;
        RawWindowHandle::Win32(handle)
    }
}

pub fn show_info_message(title: &str, description: &str) {
    let dialog = rfd::AsyncMessageDialog::new()
        .set_parent(&ForegroundWindow)
        .set_level(rfd::MessageLevel::Info)
        .set_title(title)
        .set_description(description)
        .show();
    std::thread::spawn(move || {
        block_on(dialog);
    });
}

pub fn show_error_message(title: &str, description: &str) {
    rfd::MessageDialog::new()
        .set_parent(&ForegroundWindow)
        .set_level(rfd::MessageLevel::Error)
        .set_title(title)
        .set_description(description)
        .show();
}

pub fn show_confirm_message(title: &str, description: &str) -> bool {
    rfd::MessageDialog::new()
        .set_parent(&ForegroundWindow)
        .set_level(rfd::MessageLevel::Warning)
        .set_title(title)
        .set_description(description)
        .set_buttons(rfd::MessageButtons::YesNo)
        .show()
}
